package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.os.Message
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.WindowManager
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.global.UserInfoSharePreKey
import cc.vv.baselibrary.util.LKPrefUtil
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.globle.BroadCastJniKey
import cc.vv.btpartyjob.jnimodule.moudel.resp.OrgDetailResponseObj
import cc.vv.btpartyjob.jnimodule.server.JniDataServer
import cc.vv.btpartyjob.jnimodule.server.ResourceChooseServer
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.delegate.AddPromiseActivityDelegate
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.moudel.resp.SubmitUnitReportResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 添加承诺
 * Created by Chen.qi on 2018/10/29.
 */
class AddPromiseActivity : BaseActivityMVP<AddPromiseActivityDelegate>() {

    /**
     * 完成时间
     */
    private var longEndTime: Long? = null

    /**
     * type 0:党员
     * 1：单位
     */
    private var type: Int? = 0

    /**
     * 单位名称
     */
    private var unitName: String? = null

    private var unitId: String? = null

    override fun initData() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        try {
            type = intent.getIntExtra("type", 0)
        } catch (e: Exception) {
        }

        viewDelegate?.setTopBarTitle("添加承诺")
        if (type == 0) {
            viewDelegate?.setName(LKPrefUtil.getString(UserInfoSharePreKey.USER_NAME, ""))
        } else if (type == 1) {
            val userInfo = UserInfoManageUtil.getUserInfo()
            if (TextUtils.isEmpty(unitId)) {
                val userOrgList = userInfo?.userOrgList
                if (null != userOrgList) {
                    for (i in userOrgList.indices) {
                        if (userOrgList[i].type == 4) {
                            unitId = userOrgList[i].id
                            break
                        }
                    }
                }
            }

            JniDataServer.getOrganDetail(unitId)
        }

    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //完成时间
            closeInput()
            ResourceChooseServer.chooseData(this, handler, 9, 3)
        }, R.id.ll_appAdd_chooseCompleteTime)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //提交
            val commitedContent = viewDelegate?.getCommitedContent()

            val telPhone = viewDelegate?.getTelPhone()

            if (TextUtils.isEmpty(commitedContent) || TextUtils.isEmpty(telPhone)) {
                LKToastUtil.showToastShort("请将数据填写完整")
                return@OnClickListener
            }

            if (longEndTime == 0L) {
                LKToastUtil.showToastShort("请选择时间")
                return@OnClickListener
            }

            if (type == 0) {
                PartyManagerServer.addCommitedInfo(commitedContent, longEndTime, telPhone, true)
            } else if (type == 1) {
                PartyManagerServer.addUnitCommitedInfo(unitName, unitId, commitedContent, longEndTime, telPhone, true)
            }

        }, R.id.rl_appAdd_submit)

    }


    override fun getDelegateClass(): Class<AddPromiseActivityDelegate> {
        return AddPromiseActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onHandleMsg(msg: Message?) {
        super.onHandleMsg(msg)
        when (msg?.what) {
            9 -> {
                //结束时间
                val dateString: String = msg.obj as String
                longEndTime = LKTimeUtil.getInstance().getLongTime(dateString, "yyyy-MM-dd")
                viewDelegate?.setTime(dateString)
            }
        }
    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is SubmitUnitReportResponseObj -> {
                sendBroadcast(Intent(BroadCastJniKey.BROAD_CAST_KEY_CHENG_NUO_UPDATA))
                sendBroadcast(Intent(BroadCastJniKey.BROAD_CAST_KEY_PINGJIA))
                LKToastUtil.showToastShort("添加成功")
                finish()
            }

            is OrgDetailResponseObj -> {
                val data = obj.data
                unitName = data?.name
                viewDelegate?.setUnitName(data?.name)
            }
        }
    }


}